package com.micro.school.edu.service.impl;

import java.util.Arrays;
import java.util.List;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.core.utils.StringUtils;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.edu.convert.SchoolCollegeConvert;
import com.micro.school.edu.domain.SchoolCollege;
import com.micro.school.edu.domain.vo.SchoolCollegeVo;
import com.micro.school.edu.mapper.SchoolCollegeMapper;
import com.micro.school.edu.service.ISchoolCollegeService;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.SchoolCollegeVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * 学院信息Service业务层处理
 *
 * @author rakic
 * @date 2025-08-06
 */
@Service
public class SchoolCollegeServiceImpl implements ISchoolCollegeService
{
    @Autowired
    private SchoolCollegeMapper schoolCollegeMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;
    /**
     * 查询学院信息
     *
     * @param id 学院信息主键
     * @return 学院信息
     */
    @Override
    public SchoolCollegeVo selectSchoolCollegeById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolCollegeMapper.selectSchoolCollegeById(id);
    }

    @Override
    public SchoolCollegeVos selectSchoolCollegeByIds(Long id) {
        return schoolCollegeMapper.selectSchoolCollegeByIds(id,SecurityContextHolder.getUserTenantId());
    }

    /**
     * 查询学院信息列表
     *
     * @param schoolCollegeVo 学院信息
     * @return 学院信息
     */
    @Override
    public List<SchoolCollegeVo> selectSchoolCollegeList(SchoolCollegeVo schoolCollegeVo)
    {
        schoolCollegeVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            schoolCollegeVo.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            schoolCollegeVo.setCollegeId(SecurityContextHolderUtils.getCollegeId() != null && SecurityContextHolderUtils.getCollegeId().length > 0 ? SecurityContextHolderUtils.getCollegeId(): null);
        }
        return schoolCollegeMapper.selectSchoolCollegeList(schoolCollegeVo);
    }

    /**
     * 新增学院信息
     *
     * @param schoolCollegeVo 学院信息
     * @return 结果
     */
    @Override
    public AjaxResult insertSchoolCollege(SchoolCollegeVo schoolCollegeVo)
    {
        SchoolCollege schoolCollege = SchoolCollegeConvert.INSTANCE.convert(schoolCollegeVo);

        schoolCollege.setCreateTime(DateUtils.getNowDate());
        schoolCollege.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolCollege.setSchoolName(studentInfoVos.getSchoolName());

        List<SchoolCollege> schoolCollegeList = schoolCollegeMapper.selectSchoolCollegeByName(schoolCollege.getCollegeName(),SecurityContextHolder.getUserTenantId());
        if (schoolCollegeList != null && schoolCollegeList.size() > 0){
            return AjaxResult.error("已有同名学院");
        }else{
            schoolCollegeMapper.insertSchoolCollege(schoolCollege);
            return AjaxResult.success("插入成功");
        }

    }

    /**
     * 修改学院信息
     *
     * @param schoolCollegeVo 学院信息
     * @return 结果
     */
    @Override
    public int updateSchoolCollege(SchoolCollegeVo schoolCollegeVo)
    {
        SchoolCollege schoolCollege = SchoolCollegeConvert.INSTANCE.convert(schoolCollegeVo);
        schoolCollege.setUpdateTime(DateUtils.getNowDate());
        SchoolCollegeVo schoolCollegeVo1 = schoolCollegeMapper.selectSchoolCollegeById(schoolCollegeVo.getId());
        if (!schoolCollegeVo1.getCollegeName().equals(schoolCollegeVo.getCollegeName())){
            remoteUserService.updateAllStudentInfoCollegeName(schoolCollegeVo.getCollegeName(),schoolCollegeVo.getId(),schoolCollegeVo1.getCollegeName());
            remoteOrderService.updateAllOrderCollegeName(schoolCollegeVo.getCollegeName(),schoolCollegeVo.getId());
        }
        schoolCollegeMapper.updateSchoolCollege(schoolCollege);
        return 1;
    }

    /**
     * 批量删除学院信息
     *
     * @param ids 需要删除的学院信息主键
     * @return 结果
     */
    @Override
    public int deleteSchoolCollegeByIds(List<Long> ids)
    {
//        BeanUtils.checkIdsEmpty(ids);
        return schoolCollegeMapper.deleteBatchIds(ids);
    }

    /**
     * 删除学院信息信息
     *
     * @param id 学院信息主键
     * @return 结果
     */
    @Override
    public int deleteSchoolCollegeById(Long id)
    {
//        BeanUtils.checkIdsEmpty(id);
        return schoolCollegeMapper.deleteById(id);
    }

    @Override
    public String getAllCollegeNameList(int[] collegeIds) {
        SchoolCollegeVos schoolCollegeVos = new SchoolCollegeVos();
        schoolCollegeVos.setCollegeId(collegeIds);
        schoolCollegeVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolCollegeVos.setDeleteFlag("0");
        List<String> collegeNames = schoolCollegeMapper.getAllCollegeNameList(schoolCollegeVos);
        return Arrays.toString(collegeNames.toArray());
    }

    @Override
    public List<SchoolCollegeVos> selectSchoolCollegeLists(SchoolCollegeVos schoolCollegeVos) {
        schoolCollegeVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            schoolCollegeVos.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
        }
        return schoolCollegeMapper.getAllCollegeNameLists(schoolCollegeVos);
    }

    @Override
    public List<SchoolCollegeVos> selectSchoolCollegeLists1(SchoolCollegeVos schoolCollegeVos) {
        schoolCollegeVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        return schoolCollegeMapper.getAllCollegeNameLists(schoolCollegeVos);
    }

}
